function [signal] = individual_spike_triggered_pixelmap(frame_times, spike_times, imagestack, spike_number)

%%input: frame_times, spike_times, scanrate, imagestack(x,y,t)

cut = max(find(spike_times<frame_times(length(frame_times)-50)));

for ind = 1:cut
    spike_frames(ind) = min(find(frame_times > spike_times(ind)));
end

pre_frames = 3;
post_frames = 3;
signal = zeros(size(imagestack,1),size(imagestack,2),length(spike_frames));
for ind = 1:cut
    signal_ind_post(1:post_frames) = spike_frames(ind):spike_frames(ind)+post_frames-1;
    signal_ind_pre(1:pre_frames) = spike_frames(ind)-pre_frames:spike_frames(ind)-1;
    signal(:,:,ind) = mean(imagestack(:,:,signal_ind_post),3)-mean(imagestack(:,:,signal_ind_pre),3);
end

for ind=1:cut
    win_size=4;
    signal_smoothed = filter2(ones(win_size)/win_size^2,signal(:,:,ind));
    signal_smoothed(signal_smoothed<prctile(signal_smoothed(:),80))=0;
    
    % figure(ind+100);
    % clf;
    % imagesc(signal(:,:,spike_number));
    % title('raw spike triggered pixelmap')
    
    figure(ind+100);
    clf; colormap gray
    image_data=zeros(size(imagestack,1),size(imagestack,2),3);
    image_data(:,:,2)=(mean(imagestack,3)/max(max(mean(imagestack,3))));
    image_data(:,:,1)=signal_smoothed/max(signal_smoothed(:));
    image(image_data)
    title('smoothed spike triggered pixelmap on template')
end